let treeData = [
    {
        node: 2, children: [3, 9, 4]
    }
    , {
        node: 7, children: [2]
    },
    {
        node: 3, children: [6]
    },
    {
        node: 4, children: [5]
    },
    {
        node: 5, children: [8]
    },
    {
        node: 10, children: [11]
    }
]
function getChildren(tree = [], parent = [], result) {
    let children = []
    if (parent.length > 0) {
        for (let i = 0; i < parent.length; i++) {
            let nodeData = tree.find(item => item.node == parent[i]);
            if (nodeData) {
                children.concat(...nodeData.children);
            }
        }
        result.concat(...parent);
        getChildren(tree, children, result);
    }
}
function buildArray(tree = []) {
    let result = [];
    let parent = []
    for (let i = 0; i < tree.length; i++) {
        let node = tree[i].node;
        let node_parent = tree.find(item => item.children.includes(node))
        if (!node_parent) {
            parent.push(node);
        }
    }
    getChildren(tree, parent, result);
    return result;
}
let result = buildArray(treeData);
console.log(result);